package com.cdfahe.common.util;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

/**
 * The internationalization message manager class
 * 
 * @author liu_dawei
 * 
 */
public class MessageManager {

    /** the single instance */
    private static MessageManager instance;

    /**
     * <p>
     * The Constructors Method.
     * </p>
     * 
     */
    private MessageManager() {}

    /**
     * <p>
     * get the single instance of MessageManager.
     * </p>
     * 
     * @return the single instance
     */
    public static MessageManager getInstance() {

        if (null == instance) {
            instance = new MessageManager();
        }

        return instance;
    }

    /**
     * get Message
     * 
     * @param key properties key
     * @return the message
     */
    public String getMessage(String key) {
        return this.getMessage(key, null);
    }

    /**
     * get Message
     * 
     * @param key properties key
     * @param obj the parameters
     * @return the message
     */
    public String getMessage(String key, Object[] obj) {
        // TODO
        ApplicationContext actx = null;
        actx = WebApplicationContextUtils.getWebApplicationContext(getHttpServletRequest().getSession()
                .getServletContext());

        String value = "";
        value = actx.getMessage(key, obj, getLocale());

        if (StringUtils.isEmpty(value)) {
            value = key;
        }
        return value;
    }

    /**
     * get Message
     * 
     * @param key properties key
     * @param obj the parameters
     * @return the message
     */
    public String getMessage(String key, Object[] obj, Locale locale) {
    	 ApplicationContext actx = null;
         actx = WebApplicationContextUtils.getWebApplicationContext(getHttpServletRequest().getSession()
                 .getServletContext());
        String value = actx.getMessage(key, obj, locale);

        if (StringUtils.isEmpty(value)) {
            value = key;
        }
        return value;
    }

    /**
     * get the Locale info
     * 
     * @return the Locale info
     */
    public Locale getLocale() {

        HttpServletRequest request = getHttpServletRequest();

        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

        return localeResolver.resolveLocale(request);

    }

    /**
     * 
     * <p>
     * getIELanguage
     * </p>
     * 
     * @return
     */
    public String getIELanguage() {

        return getHttpServletRequest().getLocale().getLanguage();
    }

    /**
     * <p>
     * get the request.
     * </p>
     * 
     * @return the request
     */
    public HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if (servletRequestAttributes != null) {
            return servletRequestAttributes.getRequest();
        } else {
            return null;
        }
    }

}

